package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Sku;
import com.leyou.item.Bo.SpuBo;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {


    @Autowired
    private GoodsService goodsService;

    //根据spuid查询spu 信息
    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id) {
        Spu spu = this.goodsService.querySpuById(id);
        if (spu == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(spu);
    }


    @GetMapping("spu/page")//查询分页列表数据
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                            @RequestParam(value = "key", required = false) String key,
                                                            @RequestParam(value = "saleable", required = false) Boolean saleable) {
        PageResult<SpuBo> pagelist = goodsService.querySpuByPage(page, rows, key, saleable);
        if (pagelist == null || pagelist.getItems() == null || pagelist.getItems().size() == 0) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(pagelist);

    }

    //保存商品
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        this.goodsService.saveGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    //根据商品sku的id查询商品详情
    @GetMapping("/spu/detail/{id}")
    public ResponseEntity<SpuDetail> querySpuDetailById(@PathVariable("id") Long spuId) {
        SpuDetail spuDetail = this.goodsService.querySpuDetailById(spuId);
        if (spuDetail == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(spuDetail);
    }

    //根据商品id查询sku列表
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long spuId) {
        List<Sku> skus = this.goodsService.querySkuBySpuId(spuId);
        if (skus == null || skus.size() == 0) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(skus);
    }

    //修改商品保存
    @PutMapping("goods")
    public ResponseEntity<Void> Update(@RequestBody SpuBo spuBo) {
        this.goodsService.update(spuBo);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();//204
    }

    //删除商品
    @DeleteMapping("spu")
    public ResponseEntity<Void> deleteGoods(@RequestParam("id") Long spuId) {
        this.goodsService.deleteGoods(spuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();//204服务器成功处理，无返回内容
    }

    //下架商品
    @PutMapping("spu")
    public ResponseEntity<Void> updateGoodsDownById(@RequestParam("id") Long id) {
        this.goodsService.updateGoodsDownById(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    //下架商品
    @PutMapping("spuUp")
    public ResponseEntity<Void> updateGoodsUpById(@RequestParam("id") Long id) {
        this.goodsService.updateGoodsUpById(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    //根据sku的id查询sku
    @GetMapping("/sku/{id}")
    public ResponseEntity<Sku> querySkuBySkuId(@PathVariable("id") Long id) {
        Sku sku = this.goodsService.querySkuBySkuId(id);
        if (sku == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(sku);
    }

}
